# This controller handles the login/logout function of the site.  
class SessionsController < ApplicationController
  layout false
  # Be sure to include AuthenticationSystem in Application Controller instead
  
  # render new.rhtml
  def new
  end

  def create
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      user = User.find_by_id(session[:user_id])
      if user.login == "admin"
        redirect_to admin_home_pages_path
        flash[:login_ss] = "Logged in successfully"
      else
        cookies[:user_ip] = {:value =>request.remote_ip,:expires => 1.year.from_now}
        redirect_to "/workspaces/vote"
        flash[:login_ss] = "Logged in successfully"
      end
    else
      flash[:login_err] = "Unsucessfully to login"
      redirect_back_or_default('/')
    end
  end

  def destroy
    cookies.delete :auth_token
    reset_session
    flash[:logout] = "You have been logged out."
    redirect_back_or_default('/')
  end
end
